#include "Usart.h"


#define RXBUFFERSIZE  256     	//串口最大接收字节数256


volatile char RxBuffer_1[RXBUFFERSIZE], RxBuffer_3[RXBUFFERSIZE];
volatile uint8_t RxState_1 = 0,
				 aRxBuffer_1,                  
				 Uart1_Rx_Cnt = 0;
volatile uint8_t RxState_3 = 0,
				 aRxBuffer_3,                  
				 Uart3_Rx_Cnt = 0;
volatile unsigned char uart_data_0 = 0, uart_data_1 = 0, uart_data_2 = 0, uart_data_3 = 0;

extern Timer_typeDef timer;


////串口发送单个字符
//void uart0_send_char(char ch)
//{
//    //当串口0忙的时候等待，不忙的时候再发送传进来的字符
//    while( DL_UART_isBusy(UART_0_INST) == true );
//    //发送单个字符
//    DL_UART_Main_transmitData(UART_0_INST, ch);
//}


////串口发送字符串
//void uart0_send_string(char* str)
//{
//    //当前字符串地址不在结尾 并且 字符串首地址不为空
//    while(*str!=0&&str!=0)
//    {
//        //发送字符串首地址中的字符，并且在发送完成之后首地址自增
//        uart0_send_char(*str++);
//    }
//}


bool uart_isBusy(UART_Regs *uart, uint8_t timeout_ms) 
{
    uint32_t start_tick = timer.sys_ms_count;  // 获取当前系统时间，需实现 get_tick 函数
    while (DL_UART_isBusy(uart) == 1) 
	{  // 等待发送数据寄存器为空
        if ((timer.sys_ms_count - start_tick) >= timeout_ms) 	// 超时时间为timeout_ms
		{
            return true;  // 超时
        }
		__asm__("nop");
    }
    return false;  // 不繁忙
}



// 通用的串口发送单个字符函数
void uart_send_char(UART_Instance uart_instance, char ch)
{
    switch (uart_instance)
    {
		case Uart0:
//            while (DL_UART_isBusy(UART_0_INST) == true);
//            DL_UART_Main_transmitData(UART_0_INST, ch);
				break;
        case Uart1:
            while (DL_UART_isBusy(UART_1_INST) == true);
            DL_UART_Main_transmitData(UART_1_INST, ch);
				break;
		case Uart2:
            while (DL_UART_isBusy(UART_2_INST) == true);
			//while (uart_isBusy(UART_2_INST, 10) == true);
            DL_UART_Main_transmitData(UART_2_INST, ch);
			
//			DL_UART_Main_transmitDataBlocking(UART_2_INST, ch);
				break;
		case Uart3:
            while (DL_UART_isBusy(UART_3_INST) == true);
            DL_UART_Main_transmitData(UART_3_INST, ch);
            	break;
        default:
            // 处理无效的UART实例
				break;
    }
}


// 通用的串口发送字符串函数
void uart_send_string(UART_Instance uart_instance, char* str)
{
    while (*str != 0 && str != 0)
    {
        uart_send_char(uart_instance, *str++);
    }
}


// 通用的串口发送数据函数
void uart_send_data(UART_Instance uart_instance, uint8_t* data, uint32_t size)
{
    for (uint32_t i = 0; i < size; i++)
    {
		printf("0x%02X", data[i]);
		uart_send_char(uart_instance, data[i]);
    }
	printf("\r\n");
}


// 使用通用函数实现UART2发送函数
void Uart2Send(uint8_t *p_data, uint32_t uiSize)
{	
    uart_send_data(Uart2, p_data, uiSize);
}


//串口的中断服务函数
void UART_1_INST_IRQHandler(void)
{
    //如果产生了串口中断
    switch( DL_UART_getPendingInterrupt(UART_1_INST) )
    {
        case DL_UART_IIDX_RX://如果是接收中断
            //接发送过来的数据保存在变量中
            aRxBuffer_1 = DL_UART_Main_receiveData(UART_1_INST);
            //将保存的数据再发送出去
            //uart_send_char(Uart1, RxBuffer_1);
			
			if (RxState_1 == 0)
            {
                RxBuffer_1[Uart1_Rx_Cnt++] = aRxBuffer_1;
                if ((RxBuffer_1[Uart1_Rx_Cnt - 1] == 0x12) &&
                    (RxBuffer_1[Uart1_Rx_Cnt - 2] == 0x2C))  // head
                {
                    RxState_1 = 1;
                    Uart1_Rx_Cnt = 0;
                }
            }
            else if (RxState_1 == 1)
            {
                RxBuffer_1[Uart1_Rx_Cnt++] = aRxBuffer_1;
                if (Uart1_Rx_Cnt >= 255)
                {
                    Uart1_Rx_Cnt = 0;
                    memset(RxBuffer_1, 0x00, sizeof(RxBuffer_1));
                    RxState_1 = 0;
                    return;
                }
                else if (RxBuffer_1[Uart1_Rx_Cnt - 1] == 0x5B)
                {
                    RxBuffer_1[Uart1_Rx_Cnt - 1] = 0x00;
                    Uart1_Rx_Cnt = 0;
                    RxState_1 = 0;
                    //OLED_ShowString(2, 1, RxBuffer_1);
					OLED_ShowString(2, 1, "as");
//                    BlueTooth_Command();
					printf("%s\r\n", RxBuffer_1);
                    memset(RxBuffer_1, 0x00, sizeof(RxBuffer_1));
                }
            }
//            HAL_UART_Receive_IT(UART_2_INST, (uint8_t*)&aRxBuffer_2, 1);
            break;

        default://其他的串口中断
            break;
    }
}


void UART_2_INST_IRQHandler(void)
{
    // 如果产生了串口中断
    switch( DL_UART_getPendingInterrupt(UART_2_INST) )
    {
        case DL_UART_IIDX_RX: // 如果是接收中断
            // 将接收到的数据保存在变量中
            uart_data_2 = DL_UART_Main_receiveData(UART_2_INST);
            // 调用数据处理函数
            WitSerialDataIn(uart_data_2);
            break;

        default: // 其他的串口中断
            break;
    }
}


void UART_3_INST_IRQHandler(void)
{
    // 如果产生了串口中断
    switch( DL_UART_getPendingInterrupt(UART_3_INST) )
    {
        case DL_UART_IIDX_RX: // 如果是接收中断
            // 将接收到的数据保存在变量中
            aRxBuffer_3 = DL_UART_Main_receiveData(UART_3_INST);

			if (RxState_3 == 0)
            {
                RxBuffer_3[Uart3_Rx_Cnt++] = aRxBuffer_3;
                if ((RxBuffer_3[Uart3_Rx_Cnt - 1] == 0x12) &&
                    (RxBuffer_3[Uart3_Rx_Cnt - 2] == 0x2C))  // head
                {
                    RxState_3 = 1;
                    Uart3_Rx_Cnt = 0;
                }
            }
            else if (RxState_3 == 1)
            {
                RxBuffer_3[Uart3_Rx_Cnt++] = aRxBuffer_3;
                if (Uart3_Rx_Cnt >= 255)
                {
                    Uart3_Rx_Cnt = 0;
                    memset(RxBuffer_3, 0x00, sizeof(RxBuffer_3));
                    RxState_3 = 0;
                    return;
                }
                else if (RxBuffer_3[Uart3_Rx_Cnt - 1] == 0x5B)
                {
                    RxBuffer_3[Uart3_Rx_Cnt - 1] = 0x00;
                    Uart3_Rx_Cnt = 0;
                    RxState_3 = 0;
                    //OLED_ShowString(2, 1, RxBuffer_3);
					OLED_ShowString(4, 1, "as");
                    BlueTooth_Command();
                    memset(RxBuffer_3, 0x00, sizeof(RxBuffer_3));
                }
            }
            break;

        default: // 其他的串口中断
            break;
    }
}



int fputc(int c, FILE* stream)
{
	DL_UART_Main_transmitDataBlocking(UART_1_INST, c);
    return c;
}

int fputs(const char* restrict s, FILE* restrict stream)
{
    uint16_t i, len;
    len = strlen(s);
    for(i=0; i<len; i++)
    {
        DL_UART_Main_transmitDataBlocking(UART_1_INST, s[i]);
    }
    return len;
}

int puts(const char *_ptr)
{
	int count = fputs(_ptr, stdout);
	count += fputs("\n", stdout);
	return count;
}

void BlueTooth_Command(void)
{
	if (strcmp(RxBuffer_3, "Kp++") == 0)
	  pid_now.Kp++;
	else if (strcmp(RxBuffer_3, "Kp+0.1") == 0)
	  pid_now.Kp += 0.1;
	else if (strcmp(RxBuffer_3, "Kp+0.01") == 0)
	  pid_now.Kp += 0.01;
	else if (strcmp(RxBuffer_3, "Kp+0.001") == 0)
	  pid_now.Kp += 0.001;
	else if (strcmp(RxBuffer_3, "Kp+5") == 0)
	  pid_now.Kp += 5;
	else if (strcmp(RxBuffer_3, "Kp--") == 0)
	  pid_now.Kp--;
	else if (strcmp(RxBuffer_3, "Kp-0.1") == 0)
	  pid_now.Kp -= 0.1;
	else if (strcmp(RxBuffer_3, "Kp-0.01") == 0)
	  pid_now.Kp -= 0.01;
	else if (strcmp(RxBuffer_3, "Kp-0.001") == 0)
	  pid_now.Kp -= 0.001;
	else if (strcmp(RxBuffer_3, "Kp-5") == 0)
	  pid_now.Kp -= 5;
	else if (strcmp(RxBuffer_3, "Ki+0.1") == 0)
	  pid_now.Ki += 0.1;
	else if (strcmp(RxBuffer_3, "Ki+0.01") == 0)
	  pid_now.Ki += 0.01;
	else if (strcmp(RxBuffer_3, "Ki+0.001") == 0)
	  pid_now.Ki += 0.001;
	else if (strcmp(RxBuffer_3, "Ki+0.0001") == 0)
	  pid_now.Ki += 0.0001;
	else if (strcmp(RxBuffer_3, "Ki-0.1") == 0)
	  pid_now.Ki -= 0.1;
	else if (strcmp(RxBuffer_3, "Ki-0.01") == 0)
	  pid_now.Ki -= 0.01;
	else if (strcmp(RxBuffer_3, "Ki-0.001") == 0)
	  pid_now.Ki -= 0.001;
	else if (strcmp(RxBuffer_3, "Ki-0.0001") == 0)
	  pid_now.Ki -= 0.0001;
	else if (strcmp(RxBuffer_3, "Kd+0.1") == 0)
	  pid_now.Kd += 0.1;
	else if (strcmp(RxBuffer_3, "Kd+0.01") == 0)
	  pid_now.Kd += 0.01;
	else if (strcmp(RxBuffer_3, "Kd+0.001") == 0)
	  pid_now.Kd += 0.001;
	else if (strcmp(RxBuffer_3, "Kd-0.1") == 0)
	  pid_now.Kd -= 0.1;
	else if (strcmp(RxBuffer_3, "Kd-0.01") == 0)
	  pid_now.Kd -= 0.01;
	else if (strcmp(RxBuffer_3, "Kd-0.001") == 0)
	  pid_now.Kd -= 0.001;

  // else if (strcmp(RxBuffer_2, "SWITCH") == 0)
  // {
  //   if (PID_mode == 2)
  //   {
  //     timer.mpu_flag = 0;
  //     PID_mode = 1;
  //   }
  //   else
  //   {
  //     timer.mpu_flag = 1;
  //     PID_mode = 2;
  //   }
  // }
}